package com.tunnel.common.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * 解码器，适用于客户端和服务端的协议解码
 */
public class DecodeHandler extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        int commonType = in.readInt();
        long sequenceId = in.readLong();
        int length = in.readInt();
        ByteBuf body = in.readBytes(length);
        String bodyStr = body.toString(io.netty.util.CharsetUtil.UTF_8);
        Protocol protocol = new Protocol();
        protocol.setCommonType(commonType);
        protocol.setSequenceId(sequenceId);
        protocol.setLength(length);
        protocol.setBody(bodyStr);
        System.out.println("当前解密器解密出的协议内容如下：" + protocol.toString());
        out.add(protocol);
    }
}
